package com.springcloud.sc.ucpv5.client.util;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import com.springcloud.sc.ucpv5.client.commondata.MyGroup;
import com.springcloud.sc.ucpv5.client.commondata.MyNode;
import com.springcloud.sc.ucpv5.client.commondata.MyUser;
import com.springcloud.sc.ucpv5.client.module.datamanager.BaseDataManager;

/**
 * 同一线程池处理
 * 
 * @author Administrator
 * 
 */
public class ThreadUtil {
	// 线程池
	private ExecutorService executor_instance;

	private static ThreadUtil instance = null;

	public static ThreadUtil getInstance() {
		if (instance == null) {
			instance = new ThreadUtil();
		} else {
			if (instance.executor_instance.isShutdown()) {
				instance.executor_instance = Executors.newCachedThreadPool();
			}
		}
		return instance;
	}

	public ThreadUtil() {
		executor_instance = Executors.newCachedThreadPool();
	}

	// Callable<Object> call = new Callable<Object>(){
	// public Object call() throws Exception {
	// try {
	// } catch (Exception e) {
	// // TODO: handle exception
	// }
	// return null;
	// }
	// };
	// ExecutorServiceUtil.getInstance().Call(call);
	public void Call(Callable<Object> call) {
		executor_instance.submit(call);
	}

	public Object TimeOutCall(Callable<Object> call, int timeout) {
		Object result = null;
		try {
			// LogManager.debugInfo(DateUtil.getCurDataTime() + " 线程运行:" +
			// thread.getName());
			Future<Object> future = executor_instance.submit(call);
			result = future.get(timeout * 1000, TimeUnit.MILLISECONDS);
			// LogManager.debugInfo(DateUtil.getCurDataTime() + " 线程运行:" +
			// thread.getName() + " 结果:" + resultData.getResultString());
		} catch (TimeoutException ex) {
			// LogManager.debugWarn("操作超时:" + thread.getName());
		} catch (Exception e) {
			e.printStackTrace();
		}

		return result;
	}

	// 加载本地节点数据,利用线程池
	MyNode loadNode = null;

	//
	private CopyOnWriteArrayList<String> userList = new CopyOnWriteArrayList<String>();
	private CopyOnWriteArrayList<String> groupList = new CopyOnWriteArrayList<String>();

	public void addUserNum(String userNum) {
		if (userList.contains(userNum)) {
			return;
		}
		userList.add(userNum);
	}

	public void addGroupNum(String groupNum) {
		if (groupList.contains(groupNum)) {
			return;
		}
		groupList.add(groupNum);
	}

	public boolean isQueryAll = false;

	public void 延迟发送查询用户详细列表() {
		executor_instance.submit(new Callable<Object>() {
			public Object call() throws Exception {
				try {
					int queryAll = 0;
					while (true) {
						if (userList.size() <= 0 && groupList.size() <= 0) {
							if (!isQueryAll) {
								// 目的是将所有未查询用户详细的用户重新查询一遍
								ArrayList<MyUser> users = BaseDataManager.getInstance().userGetAll();
								for (MyUser user_tmp : users) {
									MyUser user = BaseDataManager.getInstance().userGet(user_tmp.getIdlData().UserTelno);
									user.getUserInfo();
								}
								if (queryAll == 3) {
									isQueryAll = true;
								}
								queryAll++;
							}
							Thread.currentThread().sleep(2000);
							continue;
						}
						if (groupList.size() > 0) {
							String groupNum = groupList.remove(0);
							MyGroup group = BaseDataManager.getInstance().groupGet(groupNum);
							if (group != null) {
								System.out.println("查询组信息:" + groupNum);
								group.loadGroupUser(true);
								Thread.currentThread().sleep(100);
							}

						}
						Thread.currentThread().sleep(500);
					}

				} catch (Throwable e) {
					e.printStackTrace();
				} finally {
				}

				return null;

			}
		});
	}

	// 目的是为了将节点下面的所有的子节点都加载进来,启动完毕时做一次该操作
	public void 加载子节点的节点数据(final MyNode stub) {
		final List<MyNode> nodeList = stub.getListModel().getNodeList();
		 System.out.println("待查下級節點數目:" + stub.getIdlData().NodeName + "("  + stub.getIdlData().NodeNumber + "):" + nodeList.size());
		try {
			executor_instance.submit(new Callable<Object>() {
				public Object call() throws Exception {
					try {
						for (MyNode node : nodeList) {
							if(node.equals(stub)){
								continue;
							}
							BaseDataManager.getInstance().nodeLoadData(node);
							加载子节点的节点数据(node);
							Thread.sleep(50);
						}
					} catch (Throwable e) {
						e.printStackTrace();
						return null;
					}
					return null;
				}
			});
		} catch (Throwable e) {
			e.printStackTrace();
		}
	}
}
